//
//  VideoDetailController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/29.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "VideoDetailController.h"
#import "CLPlayerView.h"
#import "AnswerDetailCommentsCell.h"

@interface VideoDetailController ()<UITableViewDelegate,UITableViewDataSource>
/**视频图片*/
@property (nonatomic,strong)UIImageView*videoImage;
/**播放按钮*/
@property (nonatomic,strong)UIButton*payBtn;
/**CLplayer*/
@property (nonatomic, strong) CLPlayerView *playerView;

/**userView*/
@property (nonatomic,strong)UserIconAndNameView*userView;
/**关注按钮*/
@property (nonatomic,strong)UIButton*guanBtn;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
/**<#属性#>*/
@property (nonatomic,strong)UITableView*listView;
/**底部输入框视图*/
@property (nonatomic,strong)CommentsBottomView*bottomView;
/**键盘上面评论输入框*/
@property (nonatomic,strong)PubulicInputToolbar*inputToolbar;

@end

@implementation VideoDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createVideoView];
    [self setNavTitleAction];
}
-(void)setNavTitleAction{
    [self addNavigationItemWithImageNames:@[@"sangedian1"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    [self addNavigationItemWithImageNames:@[@"back1"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
}
-(void)rightAction{
    [self.view endEditing:YES];
    
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectZero;
    [object showPresentingViewController:self PresentedViewController:[ShareViewController new]];

}
-(void)dealloc{
    //销毁播放器
    [self.playerView destroyPlayer];
    self.playerView = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
    [UIApplication sharedApplication].statusBarHidden = YES;
    [self setIQKeyboardManagerEnable:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavBarBackgroundAndNotClearColor];
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self setIQKeyboardManagerEnable:YES];
}
-(void)setModel:(PlayModel *)model{
    _model = model;
    [self.videoImage imageViewWithUrl:model.pictureUrl placeholderImage:@"dynamicDefut"];
}
-(void)createVideoView{
    [self.view addSubview:self.videoImage];
    [self.view addSubview:self.playerView];
    [self.view addSubview:self.userView];
    [self.view addSubview:self.listView];
    [self.view addSubview:self.bottomView];
    
    [self.videoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(210);
    }];
    [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.videoImage);
    }];
    
    [self.videoImage addSubview:self.payBtn];
    [self.payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.videoImage.mas_bottom);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
        make.top.mas_equalTo(self.userView.mas_bottom);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
    }];
    [self.view addSubview:self.inputToolbar];

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    DLogFunc;
    [self.view endEditing:YES];
}

#pragma mark UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //带有二级评论的cell
    AnswerDetailCommentsCell * oneCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];
    //只有一级评论的cell
    AnswerDetailCommentsOneCell * twoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsOneCell class])];
    UserCommentsModel * commenModel = self.dataArray[indexPath.row];
    
    if (commenModel.isTwoComments) {
        oneCell.commenModel = commenModel;
        return oneCell;
    }

    twoCell.commenModel = commenModel;
    return twoCell;
}
-(UIImageView*)videoImage{
    if(!_videoImage){
        _videoImage=[[UIImageView alloc] initWithImage:IMAGE_NAMED(@"dynamicDefut")];
        _videoImage.userInteractionEnabled = YES;
    }
    return _videoImage;
}
-(UIButton*)payBtn{
    if(!_payBtn){
        _payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_payBtn setBackgroundImage:IMAGE_NAMED(@"video_play") forState:UIControlStateNormal];
        kWeakSelf(weakSelf);
        [_payBtn addActionHandler:^{
            weakSelf.videoImage.hidden = YES;
            weakSelf.playerView.hidden = NO;
            weakSelf.playerView.url = [NSURL URLWithString:weakSelf.model.videoUrl];
            [weakSelf.playerView playVideo];
        }];
    }
    return _payBtn;
}
-(UserIconAndNameView*)userView{
    if(!_userView){
        _userView=[[UserIconAndNameView alloc] initWithFrame:CGRectZero];
        _userView.deleButton.hidden = YES;
        
        [_userView addSubview:self.guanBtn];
        [self.guanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(28);
        }];
    }
    return _userView;
}
-(UIButton*)guanBtn{
    if(!_guanBtn){
        _guanBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_guanBtn setTitle:@"已关注" forState:UIControlStateSelected];
        [_guanBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_guanBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateSelected];
        [_guanBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        [_guanBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
        [_guanBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0xFFFFFF)] forState:UIControlStateSelected];
        _guanBtn.titleLabel.font = SYSTEMFONT(14);
        ViewBorderRadius(_guanBtn, 1, 0.5, UIColorFromRGB(0x959E97));
        
        [_guanBtn addActionHandler:^{
            
        }];

    }
    return _guanBtn;
}
-(CLPlayerView*)playerView{
    if(!_playerView){
        _playerView = [[CLPlayerView alloc] initWithFrame:CGRectZero];
        //设置进度条背景颜色
        _playerView.progressBackgroundColor = [UIColor colorWithRed:53 / 255.0 green:53 / 255.0 blue:65 / 255.0 alpha:1];
        //设置进度条缓冲颜色
        _playerView.progressBufferColor = [UIColor grayColor];
        //设置进度条播放完成颜色
        _playerView.progressPlayFinishColor = UIColorFromRGB(0x009966);
        _playerView.hidden = YES;
        //    //全屏是否隐藏状态栏
    //        _playerView.fullStatusBarHidden = NO;
        //    //转子颜色
        //    _playerView.strokeColor = [UIColor redColor];
        //视频地址
//        _playerView.url = [NSURL URLWithString:self.model.videoUrl];
        //播放
//        [_playerView playVideo];
        //返回按钮点击事件回调
        [_playerView destroyPlay:^{
            
            DLog(@"播放器被销毁了");
        }];
        [_playerView backButton:^(UIButton *button) {
            DLog(@"返回按钮被点击");
        }];
        //播放完成回调
        kWeakSelf(weakSelf);
        [_playerView endPlay:^{
            //销毁播放器
            [weakSelf.playerView destroyPlayer];
            weakSelf.playerView = nil;
            DLog(@"播放完成");
        }];
    }
    return _playerView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"UserComments" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UserCommentsModel *model = [UserCommentsModel new];
            [model setValuesForKeysWithDictionary:obj];
            model.isVideo = YES;
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[AnswerDetailCommentsCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];
        [_listView registerClass:[AnswerDetailCommentsOneCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsOneCell class])];

    }
    return _listView;
}

-(PubulicInputToolbar*)inputToolbar{
    if(!_inputToolbar){
        _inputToolbar=[[PubulicInputToolbar alloc] init];
        _inputToolbar.textViewMaxLine = 3;
        _inputToolbar.fontSize = 18;
        _inputToolbar.placeholder = @"写评论...";
        _inputToolbar.isTop = YES;

        kWeakSelf(weakSelf);
        [_inputToolbar inputToolbarSendText:^(NSString * _Nonnull text) {
            // 清空输入框文字
            [weakSelf.inputToolbar bounceToolbar];
        }];
    }
    return _inputToolbar;
}
-(CommentsBottomView*)bottomView{
    if(!_bottomView){
        _bottomView=[[CommentsBottomView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        [_bottomView settextFieldEnabled:^{
            [weakSelf.inputToolbar popToolbar];
        }];

    }
    return _bottomView;
}
@end

